<template>
  <el-dialog :title="`${rows?.id ? '编辑' : '新增'}参数`" width="700px" :model-value="modelValue" @close="commEvent.onClose">
    <el-form ref="formRef" :model="formValue" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="参数名称" prop="name">
            <el-input v-model="formValue.name" placeholder="请输入参数名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数键名" prop="configKey">
            <el-input v-model="formValue.configKey" placeholder="请输入参数键名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数键值" prop="configValue">
            <el-input v-model="formValue.configValue" placeholder="请输入参数键值" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="系统内置" prop="type">
            <el-radio-group v-model="formValue.type">
              <el-radio value="Y">是</el-radio>
              <el-radio value="N">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formValue.remark" type="textarea" :rows="3" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="commEvent.onClose">取消</el-button>
      <el-button type="primary" @click="commEvent.onEnter">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { nextTick, PropType, ref, watch } from "vue"
import { ElMessage, FormInstance, FormRules } from "element-plus"
import { saveConfig } from "@/api/modules/system"
import { SystemSpace } from "@/api/interface/system"

const props = defineProps({
  modelValue: { type: Boolean, default: false },
  rows: { type: Object as PropType<SystemSpace.ConfigPage>, default: () => {} }
})
const emit = defineEmits(["update:modelValue", "refresh"])
// 参数
const formRef = ref<FormInstance>() // 表单实例
// 表单数据
const formValue = ref({
  name: "",
  type: "N",
  configKey: "",
  configValue: "",
  remark: ""
})
//  表单校验
const rules: FormRules = {
  name: [{ required: true, message: "请输入参数名称", trigger: "blur" }],
  configKey: [{ required: true, message: "请输入参数键名", trigger: "change" }],
  configValue: [{ required: true, message: "请输入参数键值", trigger: "blur" }]
}
// 事件集合
const commEvent = {
  // 关闭弹窗
  onClose: () => {
    emit("update:modelValue", false)
  },
  // 确定
  onEnter: () => {
    if (!formRef.value) return
    formRef.value.validate(async valid => {
      if (!valid) return
      const params: SystemSpace.SaveConfig = { ...formValue.value }
      if (props.rows?.id) {
        params.id = props.rows.id
      }
      const { code } = await saveConfig(params)
      if (code !== "200") return
      ElMessage.success("操作成功")
      commEvent.onClose()
      emit("refresh")
    })
  }
}
watch(
  () => props.modelValue,
  val => {
    if (val) {
      nextTick(() => {
        if (!formRef.value) return
        formRef.value.resetFields()
        if (props.rows?.id) {
          const { name, type, configKey, configValue, remark } = props.rows
          formValue.value = { name, type, configKey, configValue, remark }
        }
      })
    }
  }
)
</script>

<style lang="scss" scoped>
.applet-icon {
  width: 60px;
  height: 60px;
}
.upload-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  font-size: 20px;
  border: 1px dashed #cccccc;
}
</style>
